package com.yanqu.road.server.gameplayer.module.activity.tomb.cmd;


import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.union.UnionMember;
import com.yanqu.road.entity.union.cross.CrossUnionMember;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.tomb.Tomb;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.tomb.TombMgr;
import com.yanqu.road.server.gameplayer.module.activity.tomb.TombModule;
import com.yanqu.road.server.gameplayer.module.activity.tomb.entity.DbTombInfo;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.union.UnionModule;
import com.yanqu.road.server.gameplayer.module.vehicle.VehicleModule;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.union.CrossUnionMgr;
import com.yanqu.road.server.manger.union.UnionMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.sync.message.SyncResponse;



import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

@Cmd(code = GameProtocol.S_TOMB_ACTIVITY_INFO, desc = "获取秦始皇陵活动信息,同时用来上传用户数据")
public class TombActivityInfo extends AbstractCommand {

    private Tomb.TombActivityInfoReq reqMsg;

    private Tomb.TombActivityInfoResp.Builder respBuilder;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        this.reqMsg = Tomb.TombActivityInfoReq.parseFrom(packet.getBytes());
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        this.respBuilder = Tomb.TombActivityInfoResp.newBuilder();
        ActivityInfo activityInfo = TombMgr.getActivityInfo();
        if (activityInfo == null) {
            this.respBuilder.setRet(GameErrorCode.E_ACTIVITY_NOT_OPEN);
            player.sendPacket(ClientProtocol.U_TOMB_ACTIVITY_INFO, this.respBuilder);
            return;
        }

        //更新数据
        Tomb.TombActivityInfoToCrossReq.Builder reqBuilder = Tomb.TombActivityInfoToCrossReq.newBuilder();
        TombModule tombModule = player.getModule(TombModule.class);
        List<Integer> tombSkillAddition = tombModule.getTombSkillAddition();
        reqBuilder.setAttackAddition(tombSkillAddition.get(0));
        reqBuilder.setHpAddition(tombSkillAddition.get(1));

        reqBuilder.setActivityId(activityInfo.getActivityId());
        reqBuilder.setPlayerBaseData(tombModule.getPlayerBaseTempBuilder());

        DbTombInfo dbTombInfo = tombModule.getDbTombInfo(TombMgr.getActivityInfo().getActivityId());
        if (dbTombInfo.getPartnerIdList().size() == 0) {
            for (Tomb.PartnerTemp partnerTemp : tombModule.getPartnerList()) {
                dbTombInfo.getPartnerIdList().add(partnerTemp.getId());
                reqBuilder.addPartnerList(partnerTemp);
            }

            dbTombInfo.refreshUpdateTime();
        } else {
            reqBuilder.addAllPartnerList(tombModule.getUpdatePartnerList());
        }


        CompletableFuture<SyncResponse> syncResponseFuture = GamePlayerMgr.sendToActivityCrossServerForUnionGroup(player.getUserId(), CrossProtocol.C_TOMB_ACTIVITY_INFO_TO_CROSS, activityInfo.getActivityId(), reqBuilder, this.respBuilder);

        String unionUid = player.getUserInfo().getUnionUid();

        Map<Long, CrossUnionMember> memberMap = CrossUnionMgr.getUnionMemberMap(unionUid);
        if (memberMap != null) {
            if (!memberMap.isEmpty()) {
                for (UnionMember unionMember : memberMap.values()) {
                    this.respBuilder.addUnionName(unionMember.getUserBaseInfo().getNickName());
                }
            }
        }
        this.respBuilder.setPersonalScore(dbTombInfo.getScore());
        this.respBuilder.setUnionScore(dbTombInfo.getUnionScore());
        this.respBuilder.setRelicPoint(tombModule.getTotalRelicScore());
//        tombModule.updateScoreAndUnionScore(dbTombInfo.getScore(), dbTombInfo.getUnionScore());
        //先返回客户端消息
        player.sendPacket(ClientProtocol.U_TOMB_ACTIVITY_INFO, this.respBuilder);
    }

    @Override
    public void sendData(GamePlayer player) {

    }

    @Override
    public boolean isDebug() {
        return true;
    }

    @Override
    public String getRequestData() {
        return this.reqMsg.toString();
    }
}
